//
//  Find_ContentTableViewCell.m
//  SoundPill
//
//  Created by 宋旭东 on 15/6/10.
//  Copyright (c) 2015年 宋旭东. All rights reserved.
//

#import "Find_ContentTableViewCell.h"
#import "Find_Content_Button.h"
#import "UIImageView+WebCache.h"
#import "DDPageControl.h"
#import "AppDelegate.h"
#define kScreenWidth [UIScreen mainScreen].bounds.size.width
#define kScreenHeight [UIScreen mainScreen].bounds.size.height
@interface Find_ContentTableViewCell ()<UIScrollViewDelegate>
{
    int pageCount;
    DDPageControl *workPage;
}
@property (strong, nonatomic) UIScrollView *workScroll;
@property (strong, nonatomic) UIView *buttonView1;
@property (strong, nonatomic) UIView *buttonView2;
@property (strong, nonatomic) UIView *buttonView3;
@property (strong, nonatomic) UIView *buttonView4;
@property (strong, nonatomic) UIImageView *topic;
@property (strong, nonatomic) UIButton *createWorkBtn;
@property (strong, nonatomic) UILabel *createWorkLab;
@end

@implementation Find_ContentTableViewCell
- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
//        [self createSubviews:nil];
    }
    return self;
}
- (void)removeSubviews
{
    [_reason removeFromSuperview];
    [_detile removeFromSuperview];
    [_workScroll removeFromSuperview];
    [workPage removeFromSuperview];
    [_topic removeFromSuperview];
    [_createWorkBtn removeFromSuperview];
    [_createWorkLab removeFromSuperview];
}
- (void)createSubviews:(Find_Content_Model *)model
{
    NSString *tag = nil;
    if ([model.usertag isEqualToString:@"0"]) {
        tag = [NSString stringWithFormat:@"%@",model.tag];
        
        self.topic = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"find_topic"]];
        if ([UIScreen mainScreen].bounds.size.height == 667) {
            //6
            self.reason = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth * 0.085, 0, kScreenWidth - (kScreenWidth * 0.059 * 2) - kScreenWidth * 0.2,  kScreenHeight * 0.32571 * 0.216)];
            _topic.frame = CGRectMake(kScreenWidth * 0.026, 0, kScreenHeight * 0.03, kScreenHeight * 0.03);
        } else if ([UIScreen mainScreen].bounds.size.height == 736) {
            self.reason = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth * 0.085, 0, kScreenWidth - (kScreenWidth * 0.059 * 2) - kScreenWidth * 0.2,  kScreenHeight * 0.32571 * 0.216)];
            _topic.frame = CGRectMake(kScreenWidth * 0.026, 0, kScreenHeight * 0.03, kScreenHeight * 0.03);
        } else {
            self.reason = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth * 0.09, 0, kScreenWidth - (kScreenWidth * 0.059 * 2) - kScreenWidth * 0.2,  kScreenHeight * 0.32571 * 0.216)];
            _topic.frame = CGRectMake(kScreenWidth * 0.026, 0, kScreenHeight * 0.031, kScreenHeight * 0.031);
            
        }
        _topic.center = CGPointMake(kScreenWidth * 0.026 + kScreenHeight * 0.018, _reason.center.y);
        _reason.userInteractionEnabled = NO;
        [self addSubview:_topic];
    } else {
        tag = [NSString stringWithFormat:@"#%@#",model.tag];
        self.reason = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth * 0.039, 0, kScreenWidth - (kScreenWidth * 0.039 * 2) - kScreenWidth * 0.2,  kScreenHeight * 0.32571 * 0.216)];
    }
    _reason.userInteractionEnabled = YES;
    UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTopicToSearchLabel:)];
    [_reason addGestureRecognizer:tapRec];
    _reason.font = [UIFont boldSystemFontOfSize:16];
    _reason.text = tag;
//    _reason.backgroundColor = [UIColor yellowColor];
    [self addSubview:_reason];
    if (model.topicDetail.length > 0) {
        self.detile = [[UILabel alloc] initWithFrame:CGRectMake(kScreenWidth *0.039, _reason.frame.size.height - 10, _reason.frame.size.width, 20)];
        _detile.userInteractionEnabled = YES;
        _detile.text = model.topicDetail;
        //    _detile.backgroundColor = [UIColor redColor];
        _detile.font = [UIFont systemFontOfSize:12.0f];
        _detile.textColor = [UIColor colorWithRed:153 / 255.0 green:153 / 255.0 blue:153 / 255.0 alpha:1.0];
        [self addSubview:_detile];
        UITapGestureRecognizer *tapRec = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(selectTopicToSearchLabel:)];
        [_detile addGestureRecognizer:tapRec];
    }
    

    
    NSMutableArray *works = nil;
    if (model.works.count > 12) {
        works = [NSMutableArray array];
        for (int i = 0; i < 12; i++) {
            [works addObject:model.works[i]];
        }
    } else {
        works = [NSMutableArray arrayWithArray:model.works];
    }
    self.workScroll = [[UIScrollView alloc] init];
    if (model.topicDetail.length > 0) {
        _workScroll.frame = CGRectMake(0, _reason.frame.origin.y + _reason.frame.size.height + 20, kScreenWidth, kScreenHeight * 0.32571 * 0.784);
    } else {
        _workScroll.frame = CGRectMake(0, _reason.frame.origin.y + _reason.frame.size.height, kScreenWidth, kScreenHeight * 0.32571 * 0.784);
    }
    _workScroll.pagingEnabled = YES;
    
    if (0 < works.count && works.count <= 3) {
        pageCount = 1;
    } else if (4<= works.count && works.count <= 6) {
        pageCount = 2;
    } else if (7 <= works.count && works.count <=9) {
        pageCount = 3;
    } else if (10 <= works.count && works.count <=12) {
        pageCount = 4;
    }  else {
        
    }
    _workScroll.contentSize = CGSizeMake(kScreenWidth  * pageCount, 0);
    _workScroll.delegate = self;
    _workScroll.showsHorizontalScrollIndicator = NO;
    [self addSubview:_workScroll];
    
    workPage = [[DDPageControl alloc] init];
    [workPage setNumberOfPages:pageCount];
    [workPage setCurrentPage: 0];
    [workPage setDefersCurrentPageDisplay: YES] ;
    [workPage setType: DDPageControlTypeOnFullOffEmpty] ;
    [workPage setOnColor: [UIColor blackColor]] ;
    [workPage setOffColor: [UIColor blackColor]] ;
    [workPage setIndicatorDiameter: 7] ;
    [workPage setIndicatorSpace: 5.0f] ;
    [workPage setCenter:CGPointMake(kScreenWidth - kScreenWidth * 0.026 - kScreenWidth * 0.1 + 10, _reason.frame.size.height / 2)];
    
    [self addSubview:workPage];
    
    switch (pageCount) {
        case 1:
            self.buttonView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView1];
            break;
        case 2:
            self.buttonView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView1];
            self.buttonView2 = [[UIView alloc] initWithFrame:CGRectMake(_workScroll.frame.size.width, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView2];
        case 3:
            self.buttonView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView1];
            self.buttonView2 = [[UIView alloc] initWithFrame:CGRectMake(_workScroll.frame.size.width, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView2];
            self.buttonView3 = [[UIView alloc] initWithFrame:CGRectMake(2 * _workScroll.frame.size.width, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView3];
        case 4:
            self.buttonView1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView1];
            self.buttonView2 = [[UIView alloc] initWithFrame:CGRectMake(_workScroll.frame.size.width, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView2];
            self.buttonView3 = [[UIView alloc] initWithFrame:CGRectMake(2 * _workScroll.frame.size.width, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView3];
            self.buttonView4 = [[UIView alloc] initWithFrame:CGRectMake(3 * _workScroll.frame.size.width, 0, _workScroll.frame.size.width, _workScroll.frame.size.height)];
            [_workScroll addSubview:_buttonView4];
        default:
            
            break;
    }

    //创建按钮
    for (int i = 0; i < works.count; i++) {

        if (i < 3) {
            Find_Content_Button *work = [[Find_Content_Button alloc] initWithFrame:CGRectMake((i + 1) * kScreenWidth * 0.039 + kScreenWidth * 0.28125 * i , 0, kScreenWidth * 0.28125, _workScroll.frame.size.height)];
            NSString *workId = [works[i] valueForKey:@"id"];

                work.tag = workId.intValue;

            NSString *imagestr = [works[i] valueForKey:@"cover"];
            [work.cover setImageWithURL:[NSURL URLWithString:imagestr] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
            work.workName.text = [works[i] valueForKey:@"title"];
            work.playCount.text = [works[i] valueForKey:@"view_count"];
            work.loveCount.text = [works[i] valueForKey:@"love_count"];
            [work addTarget:self action:@selector(goToPlayer:) forControlEvents:UIControlEventTouchUpInside];
            [_buttonView1 addSubview:work];
        } else if (i >= 3 && i < 6) {
            Find_Content_Button *work = [[Find_Content_Button alloc] initWithFrame:CGRectMake((i - 2) * kScreenWidth * 0.039 + (i-3) * kScreenWidth * 0.28125, 0, kScreenWidth * 0.28125, _workScroll.frame.size.height)];
            NSString *workId = [works[i] valueForKey:@"id"];
            work.tag = workId.intValue;
            NSString *imagestr = [works[i] valueForKey:@"cover"];
            [work.cover setImageWithURL:[NSURL URLWithString:imagestr] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
            work.workName.text = [works[i] valueForKey:@"title"];
            work.playCount.text = [works[i] valueForKey:@"view_count"];
            work.loveCount.text = [works[i] valueForKey:@"love_count"];
            [work addTarget:self action:@selector(goToPlayer:) forControlEvents:UIControlEventTouchUpInside];
            [_buttonView2 addSubview:work];
        } else if (i >= 6 && i < 9) {
            Find_Content_Button *work = [[Find_Content_Button alloc] initWithFrame:CGRectMake((i - 5) * kScreenWidth * 0.039 + (i-6) * kScreenWidth * 0.28125, 0, kScreenWidth * 0.28125, _workScroll.frame.size.height)];
            NSString *workId = [works[i] valueForKey:@"id"];
            work.tag = workId.intValue;
            NSString *imagestr = [works[i] valueForKey:@"cover"];
            [work.cover setImageWithURL:[NSURL URLWithString:imagestr] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
            work.workName.text = [works[i] valueForKey:@"title"];
            work.playCount.text = [works[i] valueForKey:@"view_count"];
            work.loveCount.text = [works[i] valueForKey:@"love_count"];
            [work addTarget:self action:@selector(goToPlayer:) forControlEvents:UIControlEventTouchUpInside];
            [_buttonView3 addSubview:work];
        } else if (i >= 9 && i < 12) {
            Find_Content_Button *work = [[Find_Content_Button alloc] initWithFrame:CGRectMake((i - 8) * kScreenWidth * 0.039 + (i-9) * kScreenWidth * 0.28125, 0, kScreenWidth * 0.28125, _workScroll.frame.size.height)];
            NSString *workId = [works[i] valueForKey:@"id"];
            work.tag = workId.intValue;
            NSString *imagestr = [works[i] valueForKey:@"cover"];
            [work.cover setImageWithURL:[NSURL URLWithString:imagestr] placeholderImage:[UIImage imageNamed:@"placeholderImage"]];
            work.workName.text = [works[i] valueForKey:@"title"];
            work.playCount.text = [works[i] valueForKey:@"view_count"];
            work.loveCount.text = [works[i] valueForKey:@"love_count"];
            [work addTarget:self action:@selector(goToPlayer:) forControlEvents:UIControlEventTouchUpInside];
            [_buttonView4 addSubview:work];
        }
    }
    if (![model.usertag isEqualToString:@"0"]) {
        workPage.hidden = YES;
//        _workScroll.scrollEnabled = NO;
        _createWorkBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        self.createWorkLab = [[UILabel alloc] initWithFrame:CGRectZero];
        _createWorkLab.text = @"参加";
        [_createWorkBtn setBackgroundImage:[UIImage imageNamed:@"attention"] forState:UIControlStateNormal];
        _createWorkBtn.frame = CGRectMake(0, 0, 25, 25);
        _createWorkLab.frame = CGRectMake(0, 0, 40, 20);
        _createWorkLab.font = [UIFont fontWithName:@"Helvetica-Bold" size:10.0f];
        _createWorkLab.textAlignment = NSTextAlignmentCenter;
        if ([UIScreen mainScreen].bounds.size.width == 320) {
            _createWorkBtn.center = CGPointMake(workPage.center.x + 8, _reason.center.y);
            _createWorkLab.center = CGPointMake(workPage.center.x + 8, _detile.center.y);
        } else if ([UIScreen mainScreen].bounds.size.width == 375){
            _createWorkBtn.center = CGPointMake(workPage.center.x + 10, _reason.center.y);
            _createWorkLab.center = CGPointMake(workPage.center.x + 10, _detile.center.y);
        } else {
            _createWorkBtn.center = CGPointMake(workPage.center.x + 10,_reason.center.y);
            _createWorkLab.center = CGPointMake(workPage.center.x + 10, _detile.center.y);
        }
//         _createWorkBtn.center = CGPointMake(workPage.center.x + 10, _workScroll.frame.origin.y / 2);
        [self addSubview:_createWorkBtn];
        [self addSubview:_createWorkLab];
        [_createWorkBtn addTarget:self action:@selector(selectTopicToRecord:) forControlEvents:UIControlEventTouchUpInside];
    }
}
- (void)goToPlayer:(Find_Content_Button *)sender
{
    if ([self.popTip isVisible]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"hidePopview" object:nil];
    } else {
        NSString *workid = [NSString stringWithFormat:@"%ld", sender.tag];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"gotoPlayer" object: workid];
    }
}
- (void)selectTopicToRecord:(UIButton *)sender
{
    if ([self.popTip isVisible]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"hidePopview" object:nil];
    } else {
    [AppDelegate shareDelegate].startTag = @"话#题";
    [AppDelegate shareDelegate].topicString = [_reason.text stringByReplacingOccurrencesOfString:@"#" withString:@""];
    
        [[NSNotificationCenter defaultCenter] postNotificationName:@"findToRecordWithTopic" object:nil];
    }
}
- (void)selectTopicToSearchLabel:(UITapGestureRecognizer *)tap
{
    if ([self.popTip isVisible]) {
        [[NSNotificationCenter defaultCenter]postNotificationName:@"hidePopview" object:nil];
    } else {
    UILabel *label = _reason;
    [[NSNotificationCenter defaultCenter] postNotificationName:@"findToRecordWithTopic" object:label];
    }
}
- (void)scrollViewDidScroll:(UIScrollView *)ScrollView
{

    CGFloat pageWidth = ScrollView.bounds.size.width ;
    float fractionalPage = ScrollView.contentOffset.x / pageWidth ;
    NSInteger nearestNumber = lround(fractionalPage) ;
    
    if (workPage.currentPage != nearestNumber)
    {
        workPage.currentPage = nearestNumber ;
        
        // if we are dragging, we want to update the page control directly during the drag
        if ([ScrollView isDragging])
            [workPage updateCurrentPageDisplay];
    }
}
-(void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
